package com.lagou.work.task01.oom;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * tcp通信服务器端
 */

public class SoketServerTest {
    private static Boolean flag = true;

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket accept = null;
        try {
                serverSocket = new ServerSocket(8888);
            while (flag) {
                System.out.println("等待客户端连接请求...");
                accept = serverSocket.accept();
                System.out.println("客户端" + accept.getInetAddress() + "连接成功，等待发送信息...");
                new ServerThread(accept).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(serverSocket);
        }
    }

    private static void close(ServerSocket serverSocket){
        try {
            if(null != serverSocket){
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
